package videos;

import example.demo_navi.R;
import gui_View.VideosListView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import models.Library;
import models.Video;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import clicks.VideoClickListener;

import utility.Check_Net;
import utility.DbConnect;
import utility.SDialog;
import utility.StreamUtils;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class Main3 extends Activity implements VideoClickListener{

	VideosListView vl;
	DbConnect dbc;
	ArrayList<Video> al;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_3);
		vl=(VideosListView) findViewById(R.id.gvv1);
		dbc=new DbConnect(Main3.this);
		al=new ArrayList<Video>();
		vl.setOnVideoClickListener(this);

		GetYouTubeUserVideosTask gtu=new GetYouTubeUserVideosTask(responseHandler, "choa388");
		if(Check_Net.isOnLine(Main3.this)){
		new Thread(gtu).start();
		}else{
			SDialog.Dialognet(Main3.this);
		}
	}

	public void onVideoClicked(Video video) {
		// TODO Auto-generated method stub
		Intent in=new Intent(Main3.this,Procmain3.class);
		in.setData(Uri.parse(video.getUrl()));
		Log.d("Testvid3", video.getUrl());
		startActivity(in);
	}

	private class GetYouTubeUserVideosTask implements Runnable{
		
		public static final String LIBRARY = "Library";
		private final Handler replyTo;
		private final String username;
		
		public GetYouTubeUserVideosTask(Handler hl,String us){
			this.replyTo=hl;
			this.username=us;
		}
		public void run() {
			// TODO Auto-generated method stub
			try{
				HttpClient hc=new DefaultHttpClient();
				HttpUriRequest hur=new HttpGet("https://gdata.youtube.com/feeds/api/users/VietnamGameTV/live/events?v=2&alt=json&max-results=50");
				HttpResponse hpon=hc.execute(hur);
				String jstr=StreamUtils.convertToString(hpon.getEntity().getContent());
				JSONObject jo=new JSONObject(jstr);
				JSONArray ja=jo.getJSONObject("feed").getJSONArray("entry");
				List<Video> lid=new ArrayList<Video>();
				for(int i=ja.length()-1;i>0;i--){
					JSONObject jso2=ja.getJSONObject(i);
					JSONObject jidv=jso2.getJSONObject("id");
					String idv=jidv.getString("$t");
					JSONObject cont=jso2.getJSONObject("content");
					String src=cont.getString("src");
					JSONObject iscomple=jso2.getJSONObject("yt$status");
					String status=iscomple.getString("$t");
					JSONObject pubdate=jso2.getJSONObject("published");
					String date=pubdate.getString("$t");
					String tit=jso2.getJSONObject("title").getString("$t");
					lid.add(new Video(idv,tit,null,src ,0));
				}
				Library lib=new Library(username, lid);
				Bundle bd=new Bundle();
				bd.putSerializable(LIBRARY, lib);
				Message msg=Message.obtain();
				msg.setData(bd);
				replyTo.sendMessage(msg);
			}catch(ClientProtocolException cpe){
				Log.d("CLPE", cpe.getMessage());
			}catch(IOException ex){
				Log.d("IOE", ex.getMessage());
			}catch(JSONException je){
				Log.d("JSO", je.getMessage());
			}
		}
		
	}
	
	Handler responseHandler = new Handler() {
		public void handleMessage(Message msg) {
			populateListWithVideos(msg);
		};
	};

	private void populateListWithVideos(Message msg) {
		Library lib = (Library) msg.getData().get(GetYouTubeUserVideosTask.LIBRARY);
		for(Video i:lib.getVideos()){
			dbc.insert_video(new Video(i.getVid(), i.getTitle(), i.getThumbUrl(), i.getUrl(), i.getPl_id()));
			Log.d("Layphay",i.getTitle());
		}
		al=dbc.getAllVideo();
		vl.setVideos(al);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		responseHandler=null;
		super.onStop();
	}
	
}
